package com.wswy.wzcx.ui.component;


import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;
import com.che.libcommon.utils.optional.Optional;
import com.facebook.common.util.UriUtil;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.stfalcon.frescoimageviewer.ImageViewer;
import com.wswy.wzcx.R;
import com.wswy.wzcx.model.UserMode;
import com.wswy.wzcx.ui.data.UserMessage;
import com.wswy.wzcx.ui.data.UserMessage.Message;
import com.wswy.wzcx.utils.FrescoUtils;
import com.wswy.wzcx.widget.bubbleview.BubbleDrawable.ArrowLocation;
import com.wswy.wzcx.widget.bubbleview.BubblePostprocessor;
import io.reactivex.functions.Function;
import io.reactivex.observers.ResourceSingleObserver;
import java.io.File;

class BaseFeedbackComponent<T> extends DataBindingComponent<UserMessage.Message> implements
    OnClickListener {


  SimpleDraweeView ivHeader;
  UserMode userMode;

  TextView tvContent;
  ImageView imageContent;
  TextView tvTime;

  public BaseFeedbackComponent(ViewGroup parent, int resId) {
    super(parent, resId);
  }

  @Override
  public void bind(Message data) {
    super.bind(data);
    tvTime.setText(data.getCreateTimeStr());
    if(TextUtils.isEmpty(data.image)){
      imageContent.setVisibility(View.GONE);
      tvContent.setVisibility(View.VISIBLE);
      tvContent.setText(data.content);
    }else {
      tvContent.setVisibility(View.GONE);
      imageContent.setVisibility(View.VISIBLE);
    }
  }

  @Override
  public void onClick(View v) {
    String image = (String) v.getTag(R.id.item_data);

    if(!TextUtils.isEmpty(image)){
      String[] urls = new String[1];
      if(image.startsWith("/")){
        urls[0] = UriUtil.getUriForFile(new File(image)).toString();
      }else {
        urls[0] = image;
      }
      new ImageViewer.Builder<>(v.getContext(),urls).show();
    }
  }



  protected void displayImage(String img,boolean right){
    ImageRequestBuilder requestBuilder = null;
    if(img.startsWith("/")){
      requestBuilder = ImageRequestBuilder
          .newBuilderWithSource(UriUtil.getUriForFile(new File(img)));
    }else {
      requestBuilder = ImageRequestBuilder
          .newBuilderWithSource(UriUtil.parseUriOrNull(img));
    }

    imageContent.setTag(R.id.item_data,img);
    imageContent.setOnClickListener(this);

    requestBuilder
        .setPostprocessor(new BubblePostprocessor(img, right?ArrowLocation.RIGHT:ArrowLocation.LEFT));

    FrescoUtils.loadBitmap(requestBuilder.build()).map(
        new Function<Optional<Bitmap>, Optional<Bitmap>>() {
          @Override
          public Optional<Bitmap> apply(Optional<Bitmap> bitmapOptional) throws Exception {
            Bitmap bm = bitmapOptional.orNull();
            if(bm != null){
              return Optional.ofNullable(bm.copy(bm.getConfig(),false));
            }
            return Optional.absent();
          }
        }).subscribe(
        new ResourceSingleObserver<Optional<Bitmap>>() {
          @Override
          public void onSuccess(Optional<Bitmap> bitmapOptional) {
            Bitmap bm = bitmapOptional.orNull();
            if (bm != null) {

              LayoutParams layoutParams = imageContent.getLayoutParams();
              layoutParams.width = bm.getWidth();
              layoutParams.height = bm.getHeight();
              imageContent.setLayoutParams(layoutParams);

              imageContent.setImageBitmap(bm);
            }
          }

          @Override
          public void onError(Throwable e) {

          }
        });
  }

}
